package com.tiancheng.onecode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiancheng.onecode.entity.Platform;
import com.tiancheng.onecode.entity.Role;
import com.tiancheng.onecode.entity.UserRole;
import com.tiancheng.onecode.exception.Asserts;
import com.tiancheng.onecode.mapper.PlatformMapper;
import com.tiancheng.onecode.mapper.RoleMapper;
import com.tiancheng.onecode.model.request.BasePageReq;
import com.tiancheng.onecode.model.response.PageInfo;
import com.tiancheng.onecode.service.IPlatformService;
import com.tiancheng.onecode.service.IRoleService;
import com.tiancheng.onecode.service.IUserRoleService;
import jakarta.annotation.Resource;
import org.bouncycastle.cms.PasswordRecipientId;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Author: kellen
 * @Description:角色管理 service impl
 * @Date: create in 2024/12/9 18:22
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Resource
    private IUserRoleService userRoleService;

    @Override
    public PageInfo<Role> queryPage(BasePageReq params) {
        final Page<Role> page = this.page(new Page<>(params.getCurrentPage(), params.getPageSize()), new LambdaQueryWrapper<Role>());
        return new PageInfo<Role>(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords());
    }

    @Override
    public Boolean delete(Long id) {
        final List<UserRole> roles = userRoleService.list(new LambdaQueryWrapper<UserRole>().eq(UserRole::getRoleId, id));
        Asserts.isTrue(CollectionUtils.isEmpty(roles), "角色已分配到用户，不允许删除！");
        return this.removeById(id);
    }


}
